package com.hdit.guimei.web;

import com.hdit.guimei.domain.AllCollection;
import com.hdit.guimei.service.AllCollectionService;
import com.hdit.guimei.util.ResponseData;
import com.hdit.guimei.domain.Collection;
import com.hdit.guimei.domain.Goods;
import com.hdit.guimei.service.impl.CollectionServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hdit.guimei.domain.AllCollection;
import com.hdit.guimei.service.AllCollectionService;
import com.hdit.guimei.util.ResponseData;


import java.util.List;
import java.util.List;

@Controller
public class CollectionController {
    @Autowired
    private CollectionServiceImp collectionServiceImp;
    /**
     *@methodName  Addcollection
     *@方法功能描述：添加收藏商品
     *@params  [id, good_id]
     *@return  java.lang.String
     *@author   张宝玉
     *@Date    2021/2/23 15:33
     */
    @RequestMapping("/AddCollection")
    @ResponseBody
    public String Addcollection(int id, int good_id) {
        List<Collection> list = collectionServiceImp.FindCollGood_id(id);
        String mes=null;
        int state = 1;
        int a=0;
        while (a<=list.size()){
           if (good_id==list.get(a).getGood_id()){
               state=0;
               break;
           }
           a++;
        }
        if (state==1){
            collectionServiceImp.AddColl(id, good_id);
            mes="ok";
        }else if(state ==0){
            mes="no";
        }
//        for (int a = 0;a<list.size();a++){
//          if(good_id==list.get(a).getGood_id()){
//                collectionServiceImp.AddColl(id, good_id);
//               mes="ok";
//               break;
//            }
        return mes;
    }
    @RequestMapping("/FindCollection")
    @ResponseBody
    public List<Goods> FindCollection(int id){
        List<Goods> list = collectionServiceImp.FindCollGood_detail(id);
        return list;
    }
    @RequestMapping("/DeleCollection")
    @ResponseBody
    public int  DeleCollection(int id,int good_id){
        int i = collectionServiceImp.DeleCollection(id, good_id);
        return i;
    }
    @Autowired
    private AllCollectionService allCollectionService;

    @RequestMapping("/delCollectionById")
    @ResponseBody
    ResponseData<Integer>  delCollection(int id){
        int i = allCollectionService.delCollection(id);
        ResponseData<Integer> res = new ResponseData<>();
        res.setCode(200);
        res.setMess("ok");
        res.setData(i);
        return  res;
    }

    @RequestMapping("/findAllCollection")
    @ResponseBody
    ResponseData<List> findAllCollection(){
        List<AllCollection> allCollections = allCollectionService.findAllCollection();
        ResponseData<List> res = new ResponseData<>();
        res.setCode(200);
        res.setMess("OK");
        res.setData(allCollections);
        return res;
    }

}
